package com.bitcola.security.auth.common.util.jwt;

import com.bitcola.security.auth.common.constatns.CommonConstants;
import com.bitcola.security.auth.common.util.StringHelper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.joda.time.DateTime;
import org.springframework.util.Base64Utils;

import java.io.IOException;

/**
 * Created by wx on 2017/9/10.
 */
public class JWTHelper {
    private static RsaKeyHelper rsaKeyHelper = new RsaKeyHelper();
    /**
     * 密钥加密token
     *
     * @param jwtInfo
     * @param priKeyPath
     * @param expire
     * @return
     * @throws Exception
     */
    public static String generateToken(IJWTInfo jwtInfo, String priKeyPath, int expire) throws Exception {
        String compactJws = Jwts.builder()
                .setSubject(jwtInfo.getUniqueName())
                .claim(CommonConstants.JWT_KEY_USER_ID, jwtInfo.getId())
                .claim(CommonConstants.JWT_KEY_NAME, jwtInfo.getName())
                .setExpiration(DateTime.now().plusSeconds(expire).toDate())
                .signWith(SignatureAlgorithm.RS256, rsaKeyHelper.getPrivateKey(priKeyPath))
                .compact();
        return compactJws;
    }

    /**
     * 密钥加密token
     *
     * @param jwtInfo
     * @param priKey
     * @param expire
     * @return
     * @throws Exception
     */
    public static String generateToken(IJWTInfo jwtInfo, byte priKey[], int expire) throws Exception {
        String compactJws = Jwts.builder()
                .setSubject(jwtInfo.getUniqueName())
                .claim(CommonConstants.JWT_KEY_USER_ID, jwtInfo.getId())
                .claim(CommonConstants.JWT_KEY_NAME, jwtInfo.getName())
                .setExpiration(DateTime.now().plusSeconds(expire).toDate())
                .signWith(SignatureAlgorithm.RS256, rsaKeyHelper.getPrivateKey(priKey))
                .compact();
        return compactJws;
    }

    /**
     * 公钥解析token
     *
     * @param token
     * @return
     * @throws Exception
     */
    public static Jws<Claims> parserToken(String token, String pubKeyPath) throws Exception {
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(rsaKeyHelper.getPublicKey(pubKeyPath)).parseClaimsJws(token);
        return claimsJws;
    }
    /**
     * 公钥解析token
     *
     * @param token
     * @return
     * @throws Exception
     */
    public static Jws<Claims> parserToken(String token, byte[] pubKey) throws Exception {
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(rsaKeyHelper.getPublicKey(pubKey)).parseClaimsJws(token);
        return claimsJws;
    }
    /**
     * 获取token中的用户信息
     *
     * @param token
     * @param pubKeyPath
     * @return
     * @throws Exception
     */
    public static IJWTInfo getInfoFromToken(String token, String pubKeyPath) throws Exception {
        Jws<Claims> claimsJws = parserToken(token, pubKeyPath);
        Claims body = claimsJws.getBody();
        return new JWTInfo(body.getSubject(), StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_USER_ID)), StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_NAME)));
    }
    /**
     * 获取token中的用户信息
     *
     * @param token
     * @param pubKey
     * @return
     * @throws Exception
     */
    public static IJWTInfo getInfoFromToken(String token, byte[] pubKey) throws Exception {
        Jws<Claims> claimsJws = parserToken(token, pubKey);
        Claims body = claimsJws.getBody();
        return new JWTInfo(body.getSubject(), StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_USER_ID)), StringHelper.getObjectValue(body.get(CommonConstants.JWT_KEY_NAME)));
    }

    public static void main(String[] args) throws Exception {
        var token="eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJSUFFGNUZOWTExNjQ5OSIsInVzZXJJZCI6IjMyMTI1MiIsImV4cCI6MTYyODA5NDM0N30.HYHCO9yKfbJhfaPx2_w661hbJ01vPbEtQQ-jgvAXSkGtEc2roq9E5ioVY54HrhlbtSZt_kTcdZZ4SqMkhXivbaZz4lB1A1gNkvTl0hvF35Vc6u-DyzQptPOHXUhoM8aqgeaWIYd0gvGgg1wIuUsBkQYIqONRbIYuD1IIs0WR53g";

        var p1="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCN8axGvUy8SLOFC7Dt1qirrHpWpz0a9YCELk8V4FMZzIDItkaO7YXEKxhev+ILScrTLhGrTJpDe3ApYLuwdMjkoOYQfZ2fl7xZhAI27a7iv69S1JiF/gybT5E4H03P7YMm2AwiJJ2wBc5OmuEJm6DpRr6/nOgOz+Ol8Ku511CGNwIDAQAB";
        var pub= Base64Utils.decodeFromString("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCN8axGvUy8SLOFC7Dt1qirrHpWpz0a9YCELk8V4FMZzIDItkaO7YXEKxhev+ILScrTLhGrTJpDe3ApYLuwdMjkoOYQfZ2fl7xZhAI27a7iv69S1JiF/gybT5E4H03P7YMm2AwiJJ2wBc5OmuEJm6DpRr6/nOgOz+Ol8Ku511CGNwIDAQAB");
        String pub2="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCN8axGvUy8SLOFC7Dt1qirrHpWpz0a9YCELk8V4FMZzIDItkaO7YXEKxhev+ILScrTLhGrTJpDe3ApYLuwdMjkoOYQfZ2fl7xZhAI27a7iv69S1JiF/gybT5E4H03P7YMm2AwiJJ2wBc5OmuEJm6DpRr6/nOgOz+Ol8Ku511CGNwIDAQAB";
      var i=  getInfoFromToken(token,pub);
        System.out.println(p1.equals(pub2));

    }
}
